# Contributor: Runar Tenfjord < runar dot tenfjord at gmail dot com >
# Contributor: Naveen M K <naveen521kk@gmail.com> @naveen521kk on Github

_realname=scipy
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.10.0
pkgrel=1
pkgdesc="SciPy is open-source software for mathematics, science, and engineering (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
url="https://www.scipy.org/"
license=('spdx:BSD-3-Clause')
depends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
  "${MINGW_PACKAGE_PREFIX}-openblas"
  "${MINGW_PACKAGE_PREFIX}-python-numpy")
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-python-pillow: for image saving module"
)
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-python-pytest"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
  "${MINGW_PACKAGE_PREFIX}-python-pythran"
  "${MINGW_PACKAGE_PREFIX}-xsimd"
  "${MINGW_PACKAGE_PREFIX}-meson"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pybind11"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cython"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
)
source=("${_realname}-${pkgver}.tar.gz::https://pypi.python.org/packages/source/${_realname:0:1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "001-no-static-linking.patch"
        "002-no-long-double-64.patch")
sha256sums=('c8b3cbc636a87a89b770c6afc999baa6bcbb01691b5ccbbc1b1791c7c0a07540'
            'b47c052ed40cbbe7412a927ee92925292196fe712d29a60755e198c5e9c68fca'
            '52a5f81bf0c9fe37763517ba7703063c034247be935ea09c4ce014440dca5d3d')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/001-no-static-linking.patch"
  patch -Np1 -i "${srcdir}/002-no-long-double-64.patch"
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/meson \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install

  # https://github.com/msys2/MINGW-packages/pull/12386#issuecomment-1201661997
  _purelib="$(python -c "import sysconfig; print(sysconfig.get_path('purelib'))")"
  _site_packages="${pkgdir}$(cygpath "${_purelib}")"
  cp "${srcdir}/${_realname}-${pkgver}/PKG-INFO" "${_site_packages}/${_realname}-${pkgver}.egg-info"

  find "${pkgdir}${MINGW_PREFIX}" -name '*.dll.a' -delete

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}
